Repository navigation
fix(functions): keep the raw request body and leave req.body undefined, as deployed functions do - #328
Conversation
…d, as deployed functions do
Deployed AWS functions (lambda-node-bridge) hand the function a real
request stream that still holds the body, and a req.body parsed from the
same bytes: undefined with no content type, JSON, a Buffer for
octet-stream, querystring for urlencoded, the string for text/plain, and
undefined for any other type. Serve used Express's body parsers, which
read the stream, so a function reading it (a webhook signature check)
got nothing, one waiting for 'end' on a text/plain body hung, and
req.body was {} with no body or an unknown type.
Serve now reads the body once, up to the 6mb cap, and puts the bytes back
into the request stream with unshift before 'end' is emitted, then sets
req.body by the bridge's rules. A request with no body is not touched.
next() runs on the next tick, so a handler that pipes the request and
returns still starts flowing before serve ends the response, as before.
Kept from gap 1: a malformed JSON body is a bare 400, and a body over
the cap a bare 413; the rest of an oversized body is drained so a
keep-alive connection answers its next request. A client that drops the
connection mid-body is answered as a request error without the function
running.
AGENTS.md describes the change, and the .talismanrc checksums of the two
touched files are refreshed.
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
dhruv-parekh-cs
left a comment
There was a problem hiding this comment.
Reviewed the whole PR · pass 1/3: 0 high, 0 medium, 3 low (inline).
Checked the body handling against lambda-node-bridge/cf-sv-with-helpers.ts: plain JSON.parse, no charset/BOM or Content-Encoding handling and undefined for other types all match the bridge, so those differences from body-parser are not raised.
Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#502): no issues. The e2e expectations (text/plain readable from 'end', application/xml → req.body undefined) match what this PR serves; #502's serve run (29/30) needs this PR merged first.
— 🤖 launch-pr-review-agent (Claude)
Generated by Claude Code
…a hang-up as 400 Three review findings on the body handling. A client that hangs up mid-body is now rejected as a 400 request.aborted, as body-parser logged it, instead of a 500 server error. An empty chunked body no longer hangs a function that waits for the request's 'end' event. Serve waits one turn for the body to arrive and, when the request is already complete with nothing buffered, hands it to the function without reading it to the end, so 'end' still fires for the function. A body the function leaves unread is drained once its response finishes, as Node's own dump would, so the request still ends and closes. Reading the body marks the request as consumed, which stopped Node from doing it. This also covers the 400 bad-JSON path. AGENTS.md describes the change, and the .talismanrc checksums of the two touched files are refreshed.
55c8c92
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
1 similar comment
🔒 Security Scan Results
⏱️ SLA Breach Summary
ℹ️ Vulnerabilities Without Available Fixes (Informational Only)The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:
✅ BUILD PASSED - All security checks passed |
Brings in the functions fixes from #327, #328 and #329. The only conflict was .talismanrc, the AGENTS.md checksum line: both sides had changed it, so it is recomputed with Talisman for the merged AGENTS.md. All of our other .talismanrc entries are kept as they were. #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
What was wrong
Deployed AWS functions (
lambda-node-bridge/cf-sv-with-helpers.ts) run the function on a real Nodehttp.Serverrequest that still holds the body.req.bodyis a separate lazy getter parsed from the same bytes:req.bodyon AWScontent-typeundefinedapplication/jsonJSON.parse(400 if it fails)application/octet-streamBufferapplication/x-www-form-urlencodedquerystring.parsetext/plainundefinedlaunch:functions:serveused Express'sjson,urlencoded,textandrawparsers. They read the request stream before the function ran, which caused three problems:for await,'data'/'end',pipe) got nothing, which breaks webhook signature checks.'end'on atext/plainbody hung. The parser had already consumed the stream and'end'had fired. Deployed, that is a 504.req.bodywas{}when there is no body or the type is unknown.This is CL-8970 gap 8.
The change (
src/functions/cloud-functions.ts)The four Express parsers are replaced by one reader,
readBodyLikeDeployedFunctions:transfer-encoding, and no or zerocontent-length). Itsreq.bodyisundefined.request.unshift()before'end'is emitted, so the function can read the raw stream andreq.body.req.bodyis set by the bridge's table above. Types are matched with Express'srequest.is(...). An empty body isundefinedfor every type, so an empty JSON body is not a 400.setImmediatefor the body to arrive. A request that is thencompletewith nothing buffered goes to the function unread, so'end'is still pending for a function that waits for it._dumpwould (only when nothing is reading it). Reading the body marks the request as consumed, which stops Node from doing that itself, so'end'and'close'never fired.next()runs on the next tick. A handler that pipes the request into its response and returns still starts flowing before serve ends the response (gap 4), as when the handler ran straight from the request event.Request rejected before the function ran: …. After a 413 the rest of the upload is drained, so a keep-alive connection answers its next request. A client that drops the connection mid-body is logged as a 400request.aborted, as body-parser logged it, and the function does not run.AGENTS.md. The approved V1 differences describe the raw stream and theundefinedbody, and what Azure and GCP (V1) do..talismanrc. Refreshed checksums forcloud-functions.test.tsandAGENTS.md(test code and prose, no credentials).Tests
src/functions/cloud-functions.test.ts, new or extended:hands the handler an undefined body for … with no body: a GET, an empty JSON POST and an empty chunked JSON POST.hands the handler an undefined body for a content type deployed functions do not parse(application/xml).keeps a {application/json,text/plain} body in the request stream as well as in request.body.lets a handler that waits for the end event read a text/plain body instead of hanging.answers the next request on a keep-alive connection when the handler leaves the body unread.answers the next request on a keep-alive connection after refusing a body over the payload limit.keeps serving after a client drops its connection in the middle of sending a body.hands the handler an undefined body for an empty chunked JSON body whose end arrives after its headers.lets a handler that waits for the end event answer an empty chunked body of a type serve does not parse.ends and closes a request whose large body the handler leaves unread once its response is sent on a keep-alive connection, as Node does.tags=a&tags=b(it already did after gap 6). The gap 1 400/413 tests are unchanged and pass.Proof:
unshiftfails 4 tests;next()directly fails the echo test;resume()after a 413 fails the keep-alive-after-413 test;'error'listener fails the disconnect test;parseBodyfails the late-terminator chunked JSON test.Full suite in the pre-push hook: 189 suites, 3844 tests passed. Every hook ran (Snyk included), with no
--no-verify. The content-type guard (test/integration/transport-content-type.test.ts) passes: types are matched withrequest.is, so the file has noapplication/jsonliteral.Review fixes (55c8c92)
This commit fixes Dhruv's three Low findings:
request.aborted.'end'. It now goes to the function unread.Each fix has a test that fails without it.
E2E (http-sanity cloud-functions checks, AWS expectations)
Against
launch:functions:serve, with the 30 checks from e2e PR contentstack/contentfly-e2e-tests#502:a GET with no body hands req.body as undefinedgets{};the raw request stream still holds the bodygets 0 bytes;a handler that waits for the end event can read a text/plain bodytimes out;a body of a type deployed functions do not parse hands req.body as undefinedgets{}.require,__dirname).The gap 8 e2e PR, contentstack/contentfly-e2e-tests#502, adds the two new checks and a
raw-body-events.jssite function.Not changed
req.body.Content-Encoding: gzipis not inflated, as on AWS. Express honouredcharsetand inflated gzip.querystring.parsedoes. body-parser answered 413.Jira: https://contentstack.atlassian.net/browse/CL-8970
Generated by Claude Code